package chapter6.No6_1_立即加载_饿汉模式.demo9;

import java.io.*;

/**
 * 序列化与反序列化的单例模式实现
 *
 * 解决办法：就是在反序列化中使用readResolve()方法
 * Created by Administrator on 2016/11/25.
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        MyObject object = MyObject.getInstance();
        try {
            FileOutputStream file = new FileOutputStream(new File("F:\\myObject.txt"));
            ObjectOutputStream objOut = new ObjectOutputStream(file);
            objOut.writeObject(object);
            objOut.close();
            file.close();
            System.out.println(object.hashCode());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Thread.sleep(3000);

        try {
            FileInputStream file2 = new FileInputStream(new File("F:\\myObject.txt"));
            ObjectInputStream ois = new ObjectInputStream(file2);
            MyObject readObject = (MyObject)ois.readObject();
            ois.close();
            file2.close();
            System.out.println(readObject.hashCode());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
